<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>组件通信-子传父(自定义事件)</title>
    <script src="js/vue.js"></script>
</head>
<body>
<div id="app">
    <cpn @itemclick="cpnClick"></cpn>
    <h4>子组件向父子件传递： {{content}}</h4>
</div>
<template id="cpn">
    <div>
        <button v-for="(item,index) in categoties" :key="index" @click="btnClick(item)">{{item.name}}</button>
    </div>
</template>
</body>
<script>
    const cpn = {
        template:'#cpn',
        data(){
            return {
              categoties:[
                  {
                      id: 'aaa',
                      name:'热门推荐'
                  },
                  {
                      id: 'bbb',
                      name:'手机数码'
                  },
                  {
                      id: 'ccc',
                      name:'家用电器'
                  },
                  {
                      id: 'ddd',
                      name:'电脑办公'
                  }
              ]
            }
        },
        methods:{
            btnClick(item){
                this.$emit('itemclick',item);
            }
        }
    }

    const app = new Vue({
        el:"#app",
        data(){
            return{
                content:''
            }
        },
        methods: {
            cpnClick(item){
                console.log('cpnClick'+item.name);
                this.content = item.name;
            }
        },
        components: {
            cpn
        }
    })

</script>
</html>